﻿using Xray.Common;

namespace Xray.Viewer
{
    public class ModelManager
    {
        private Model _currentModel;
        private ModelDeltas _modelDeltas;

        public ModelManager(Model currentModel)
        {
            _currentModel = currentModel;
        }

        public void ApplyUpdatedModel(Model model)
        {
            ModelDeltas deltas = new ModelDeltas();


            foreach (Component component in model.Components)
            {
                if (!_currentModel.Components.Contains(component))
                {
                    deltas.NewComponents.Add(component);
                }
            }

            foreach (Component component in model.Components)
            {
                Component current = _currentModel.FindComponentByFullName(component.FullName());
                if (current == null)
                {
                    foreach (Component dependent in component.Dependents)
                    {
                        deltas.NewDependents.Add(new Pair(component, dependent));
                    }
                }
                else
                {
                    foreach (Component possibleDependent in component.Dependents)
                    {
                        if (!current.Dependents.Contains(possibleDependent))
                        {
                            deltas.NewDependents.Add(new Pair(component, possibleDependent));
                        }
                    }
                }
            }
            _currentModel = model;
            _modelDeltas = deltas;
        }

        public ModelDeltas LastModelDelta()
        {
            return _modelDeltas;
        }

    }
}